#coding=utf-8
#!/usr/bin/python

'''
Created on 2012-11-17

@author: Neo.Chen
'''

import dataconvert
import ktlib

if __name__ == '__main__':
    import ktlib
else:
    #from ktlib import ktlib
    import ktlib

#编码函数
def encoder_number(t, l, v):
    tag = t.to_bytes(1, byteorder='big')
    length = l.to_bytes(2, byteorder='big')
    value = v.to_bytes(l, byteorder='big')
    buf = tag + length + value
    return buf

def encoder_bytes(t, vBytes):
    n = len(vBytes)
    tag = t.to_bytes(1, byteorder='big')
    length = n.to_bytes(2, byteorder='big')
    buf = tag + length + vBytes
    return buf

def encoder_int8(t, v):
    return encoder_number(t, 1, v)

def encoder_int16(t, v):
    return encoder_number(t, 2, v)

def encoder_int32(t, v):
    return encoder_number(t, 4, v)

def encoder_string(t, v):
    n = len(v)
    tag = t.to_bytes(1, byteorder='big')
    length = n.to_bytes(2, byteorder='big')
    buf = tag + length + value
    return buf

#解码函数
def decode(b, t, dectype="int"):
    n = len(b)
    if n < 3:
        return 0
    pos = 0
    while pos < n:
        tag = int.from_bytes(b[pos:pos+1], byteorder='big')
        length = int.from_bytes(b[pos+1:pos+3], byteorder='big')
        if tag == t:
            values = b[pos+3:pos+3+length]
            if dectype == "int":
                return int.from_bytes(values, byteorder='big')
            elif dectype == "string":
                return bytes.decode(values)
            elif dectype == "bytes":
                return values
            break
        pos = pos + 3 + length


def to_bytes(b, t):
    return decode(b, t, dectype="bytes")

def to_int(b, t):
    return decode(b, t, dectype="int")

def to_string(b, t):
    return decode(b, t, dectype="string")

#12字节的tlv头
def make_head(cmd16, pkLen):
    version = int(3).to_bytes(1, byteorder='big')
    reserved = int(0).to_bytes(1, byteorder='big')
    length = int(pkLen).to_bytes(2, byteorder='big')
    seq_id = int(0).to_bytes(4, byteorder='big')
    cmd_id = int(cmd16).to_bytes(2, byteorder='big')
    result = int(0).to_bytes(2, byteorder='big')
    #avcp 的扩充
    #crcrst = version ^ length ^ seq_id ^ cmd_id ^ result;
    crcrst = 3 ^ ktlib.myhtonl(pkLen) ^ ktlib.myhtonl(cmd16);
    crc8t = crcrst & 0x00fe;
    reserved = int(0+crc8t).to_bytes(1, byteorder='big');

    head = version + reserved + length + seq_id + cmd_id + result
    return head

#提取12字节头信息
def parse_head(data):
    b = data[0:1]
    version = int.from_bytes(b, byteorder='big')
    b = data[1:2]
    reserved = int.from_bytes(b, byteorder='big')
    b = data[2:4]
    length = int.from_bytes(b, byteorder='big')
    b = data[4:7]
    seq_id = int.from_bytes(b, byteorder='big')
    b = data[8:10]
    cmd_id = int.from_bytes(b, byteorder='big')
    b = data[10:12]
    result = int.from_bytes(b, byteorder='big')
    return (cmd_id, length)

##################################################################################



def test_tlv():
    n = 10
    b8 = encoder_int8(1, n)
    print(b8)
    b16 = encoder_int16(2, n)
    print(b16)
    b32 = encoder_int32(3, n)
    print(b32)
    bs = encoder_string(4, "sample你去死")
    print(bs)
    head = make_head(1234, 56)

    b = b8 + b16 + b32 + bs
    k8 = to_int(b, 1)
    print(k8)
    k16 = to_int(b, 2)
    print(k16)
    k32 = to_int(b, 3)
    print(k32)
    ks = to_string(b, 4)
    print(ks)
    (cmd_id, length) = parse_head(head)


if __name__ == '__main__':
    test_tlv()



















